package com.trkj.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.trkj.dao.jpa.IStaffDao;
import com.trkj.dao.jpa.J_CardDao;
import com.trkj.dao.jpa.J_chargeprojectDao;
import com.trkj.dao.jpa.J_chargeprojectTable;
import com.trkj.dao.jpa.J_hosp_patient_registration;
import com.trkj.dao.mybatis.MPatientRegMapper;
import com.trkj.dao.mybatis.MStsffDao;
import com.trkj.dao.mybatis.M_hosp_patien;
import com.trkj.pojos.BigOffice;
import com.trkj.pojos.Card;
import com.trkj.pojos.ChargeprojectTable;
import com.trkj.pojos.DepositTable;
import com.trkj.pojos.DoctorTitle;
import com.trkj.pojos.HospPatientRegistration;
import com.trkj.pojos.SmallOffice;
import com.trkj.pojos.Staff;
import com.trkj.vo.Doctor_arrangeVo;
import com.trkj.vo.Patient_registration_manageVo;
import com.trkj.vo.Serach_condition;

@Service
public class Hosp_patien_service {
	@Autowired
	M_hosp_patien m_host;
	@Autowired
	J_chargeprojectDao j_charge;
	@Autowired
	J_CardDao j_card;
	@Autowired
	IStaffDao j_staff;
	@Autowired
	J_chargeprojectTable j_chargeproject;
	@Autowired
	J_hosp_patient_registration J_hosp;
	@Autowired
	MStsffDao m_staff;
	@Autowired
	MPatientRegMapper M_patient_reg;
	
	private long chargeCode=0;
	
	String str[]=new String[7];//获取7天日期
	private SimpleDateFormat format;
	/**
	 * 加载科室
	 * @return
	 */
	public Map<String,Object> getOffice(){
		get_seven_date();
		Map<String,Object> map=new HashMap<>();
		map.put("list", m_host.check_office());
		map.put("date",str);
		return map;
	}
	
	/**
	 * 获取七天日期
	 */
	public String[] get_seven_date(){
		Date today;
		Calendar calendar = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		for(int i=0;i<7;i++){
			if(i==0){
				calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR)); 
			}else{
				calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1); 
			}
			today= calendar.getTime();
			str[i]= format.format(today);
		}
		return str;
	}
	
	/**
	 * 加载门诊挂号信息
	 * @param patient
	 * @return
	 */
	public Map<String,Object> load_menzhen(Serach_condition patient){
		Page<DepositTable> p = PageHelper.startPage(patient.getPage(),patient.getRows(), true);
		List<Patient_registration_manageVo> list=m_host.load_menzhen(patient);
		Map<String,Object> map=new HashMap<String, Object>();
		map.put("rows", list);
		map.put("total", p.getTotal());
		map.put("page", 1);
		return map;
	}
	
	/**
	 * 根据科室查询医生排班信息
	 * @param id
	 * @return
	 * @throws ParseException 
	 */
	public List<Doctor_arrangeVo> load_doctor_by_small_office(String small_office_id,String date,String classes) throws ParseException{
		format = new SimpleDateFormat("yyyy-MM-dd");
		if(date==null||date==""){
			Date today=new Date();
			date=format.format(today);
			System.out.println(date);
		}if(classes==null||classes==""){
			classes="上午";
		}if(small_office_id==null){
			return null;
		}
		Map<String,Object> map=new HashMap<>();
		map.put("small_office_id", small_office_id);
		map.put("classes", classes);
		map.put("date",format.parse(date));
		return m_host.load_doctor_by_small_office(map);
	}
	
	/**
	 * 加载挂号收费信息
	 * @param doctor_id
	 * @return
	 */
	public Map<String,Object> get_guahao_charge_message(String charge_name){
		Map<String,Object> map=new HashMap<>();
		ChargeprojectTable charge=j_charge.findByChaegename(charge_name.trim());
		map.put("detail",charge);
		return map;
	}
	
	/**
	 * 验证诊疗卡号
	 * @param card_id
	 * @return
	 * @throws ParseException 
	 */
	public String verify_card_and_charge(String card_id,String date,String classes,String doctor_id,Long charge_id){
		Card card=j_card.findOne(card_id);
		if(card==null){
			return "该诊疗卡不存在！请重新输入";
		}else{
			SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
			Date start_date = null;
			Date end_date=null;
			try {
				start_date=format.parse(date);
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(start_date);
				calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 1);
				end_date=format.parse(format.format(calendar.getTime()));
			} catch (ParseException e) {
				System.out.println("日期转换错误");
			}
			//int reg_times=J_hosp.count_reg_times(card_id,start_date,end_date);
			int reg_times=J_hosp.countByCardAndVisitTimeBetween(card,start_date,end_date);
			if(reg_times>=3){
				return "当天挂号次数不能超过三次哦！";
			}
			return add_reg(card,date,classes,doctor_id,charge_id);
		}
	}
	
	/**
	 * 新增挂号
	 * @param card
	 * @param date
	 * @param classes
	 * @param doctor_id
	 * @param charge_id
	 * @return
	 */
	public String add_reg(Card card,String date,String classes,String doctor_id,Long charge_id){
		String maxId=J_hosp.findMaxId();
		String newId="";
		HospPatientRegistration reg=new HospPatientRegistration();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		int number=-1;
		if(classes=="上午"){
			date+=" 08:00:00";
		}else{
			date+=" 14:00:00";
		}
		try {
			reg.setVisitTime(format.parse(date));
			number=Integer.parseInt(maxId.substring(1))+1;
		} catch (ParseException e1) {
			System.out.println("日期格式转换错误");
			e1.printStackTrace();
		}catch(Exception e2){
			System.out.println("数据库除了首位含有非数字");
		}
		if(number<10){
			newId="00"+number;
		}else if(number<100){
			newId="0"+number;
		}
		newId="m"+newId;
		Staff sta=j_staff.findOne(doctor_id);
		ChargeprojectTable chage=j_chargeproject.findOne(charge_id);
		reg.setPatientRegistrationId(newId);
		reg.setStaff(sta);;
		reg.setChargeprojectTable(chage);
		reg.setCard(card);
		reg.setOperationTime(new Date());
		reg.setPayOrNot("已付");
		reg.setStatus("未看诊");
		J_hosp.save(reg);
		return "挂号成功！挂号编号："+newId;
	}
	
	
	/**
	 * 加载科室大类
	 * @return
	 */
	public List<BigOffice> get_big_office(){
		return m_host.get_big_office();
	}
	
	/**
	 * 根据科室大类加载科室小类
	 * @param big_office_id
	 * @return
	 */
	public List<SmallOffice> get_small_office(int big_office_id){
		return m_host.get_small_office(big_office_id);
	}
	
	/**
	 * 根据科室小类和日期加载医生排班
	 * @param small_office_id
	 * @param data_
	 * @return
	 */
	public List<Doctor_arrangeVo> get_doctor_arrange(int small_office_id,Date data_,String classes){
		return m_host.get_doctor_arrange(small_office_id, data_,classes);
	}
	
	/**
	 * 改号判断是否需要补钱
	 * @param oldId
	 * @param newId
	 * @return
	 */
	public String checkRegMoney(String oldId,String newId){
		Staff oldDoctor = m_staff.findByID(oldId);
		Staff newDoctor= m_staff.findByID(newId);
		String oldTitle="";
		String newTitle="";
		
		List<DoctorTitle> doctorTitles = oldDoctor.getDoctorTitles();
		List<DoctorTitle> doctorTitles2 = newDoctor.getDoctorTitles();
		for (DoctorTitle doctorTitle : doctorTitles) {
			if(doctorTitle.getName().contains("医生")){
				oldTitle=doctorTitle.getName();
			}
		}
		for (DoctorTitle doctorTitle : doctorTitles2) {
			if(doctorTitle.getName().contains("医生")){
				newTitle=doctorTitle.getName();
			}
		}
		ChargeprojectTable oldCharge=j_chargeproject.findByChaegename(oldTitle);
		ChargeprojectTable newCharge=j_chargeproject.findByChaegename(newTitle);
		chargeCode=newCharge.getChargecode();
		int compare = oldCharge.getPrice3().compareTo(newCharge.getPrice3());
		if(compare==0){
			return "不需要补钱";
		}else if(compare>0){
			return "退您："+oldCharge.getPrice3().subtract(newCharge.getPrice3())+"元";
		}else{
			return "还需要交："+newCharge.getPrice3().subtract(oldCharge.getPrice3())+"元";
		}
	}
	
	/**
	 * 改号
	 * @param date_
	 * @param classes
	 * @param reg_id
	 * @param doctor_id
	 * @return
	 */
	public String changeReg(String date,String classes,String reg_id,String doctor_id){
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(classes.equals("上午")){
			date+=" 08:00:00";
		}else{
			date+=" 14:00:00";
		}
		try {
			m_host.updatePatientReg(reg_id, doctor_id, chargeCode, format.parse(date));
			return "改号成功";
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return "改号失败";
	}
}
